package com.android.jingweiapp.fragment.topic;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.jingweiapp.R;
import com.android.jingweiapp.XMLPhase.SpecialXMLPhase;
import com.android.jingweiapp.activity.SearchActivity;
import com.android.jingweiapp.activity.TopicSecondActivity;
import com.android.jingweiapp.adapter.TopicAdapter;
import com.android.jingweiapp.bean.TopicBean;
import com.android.jingweiapp.util.Contacts;
import com.android.jingweiapp.util.HttpUtil;
import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 经信专栏
 */
public class SpecialFragment extends Fragment {

    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.btn_search)
    TextView btnSearch;
    @Bind(R.id.list)
    ListView list;

    private ArrayList<TopicBean.DataEntity> topicBeans;
    private TopicAdapter topicAdapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_special, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                String str = etSearch.getText().toString();
                intent.putExtra(Contacts.KEY.SEARCH_KEYWORD, TextUtils.isEmpty(str) ? " " : str);
                startActivity(intent);
            }
        });

        topicBeans = new ArrayList<>();
        topicAdapter = new TopicAdapter(topicBeans, getActivity());
        list.setAdapter(topicAdapter);
        list.setOnItemClickListener(new MyOnItemClickListener());

        new MyAsyncTask().execute();
    }

    private void dealData(String str) {
        Gson gson = new Gson();
        TopicBean topicBean = gson.fromJson(str, TopicBean.class);
        topicBeans.addAll(topicBean.getData());
        topicAdapter.notifyDataSetChanged();

    }

    class MyOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startActivity(new Intent(getActivity(), TopicSecondActivity.class)
                    .putExtra(Contacts.KEY.KEY_OBJECT, topicBeans.get(position))
                    .putExtra(Contacts.KEY.KEY_TYPE, 0));
        }
    }

    class MyAsyncTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {
            return HttpUtil.postWithContent(Contacts.BASE_URL,
                    SpecialXMLPhase.createParam(getActivity()),
                    new SpecialXMLPhase());
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            dealData(s);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
